package com.sky.service.impl;

import com.alibaba.druid.support.spring.stat.annotation.Stat;
import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.entity.SetmealDish;
import com.sky.mapper.*;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public OrderOverViewVO getOrderviewOrders() {
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        Integer waitingOrders = orderMapper.selectByStatusAndOrderTimeGTE(Orders.TO_BE_CONFIRMED, begin);
        Integer deliveredOrders = orderMapper.selectByStatusAndOrderTimeGTE(Orders.CONFIRMED, begin);
        Integer completedOrders = orderMapper.selectByStatusAndOrderTimeGTE(Orders.COMPLETED, begin);
        Integer cancelledOrders = orderMapper.selectByStatusAndOrderTimeGTE(Orders.CANCELLED, begin);
        Integer allOrders = orderMapper.selectByStatusAndOrderTimeGTE(null , begin);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        Integer newUsers = userMapper.getUserCount(begin, end);
        Double turnover = orderMapper.getTurnover(begin, end, Orders.COMPLETED);
        turnover = turnover == null ? 0.0 : turnover;
        Integer validOrderCount = orderMapper.getOrderCount(Orders.COMPLETED, begin, end);
        Integer totalOrderCount = orderMapper.getOrderCount(null, begin, end);

        Double unitPrice = 0.0;
        Double orderCompletionRate = 0.0;

        if(totalOrderCount != 0 && validOrderCount != 0) {
            unitPrice = turnover / validOrderCount;
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
        }

        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    @Override
    public DishOverViewVO getDishOverView() {
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE);
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Integer sold = setmealMapper.countByStatus(StatusConstant.ENABLE);
        Integer discontinued = setmealMapper.countByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
